package day0401;

import java.util.*;

class Stack36 {
    /**
     * 用两个栈实现队列
     */


    Stack<Integer> A, B;

    public Stack36() {
        A = new Stack<>();
        B = new Stack<>();
    }

    public void push(int x) {
        A.push(x);
    }

    public int pop() {
        if (B.empty()) {
            addToB();
        }
        return B.pop();
    }

    public int peek() {
        if (B.empty()) {
            addToB();
        }
        return B.peek();
    }

    public boolean empty() {
        return A.empty() && B.empty();
    }

    private void addToB() {
        while (!A.empty()) {
            B.push(A.pop());
        }
    }
}
